<?php

if(!defined('BASEPATH')) exit('No direct Script access allowed');

class department extends CI_Controller {
	function __construct(){
		parent:: __construct();
		if(!$this->ion_auth->logged_in()){
				redirect('auth/login');}
			$this->load->library('form_validation');
			$this->load->model('department/department_model');
			$this->load->library('datatables');
	}
	function get(){
			echo $this->department_model->get();
		}
	function create_department()
		{
		$this->Data['page_title'] = "Create department";
		$meta['page_title'] = "Create department";
		$this->form_validation->set_rules('name','department Name','required|xss_clean');
		$this->form_validation->set_rules('description','department Description','required|xss_clean');
		$this->form_validation->set_rules('remarks','department remarks','required|xss_clean');
		$this->form_validation->set_rules('entryby','department Entry By','required|xss_clean');
		
		if($this->form_validation->run() ==true){
				$name =strtoupper( $this->input->post('name'));
				$description = strtoupper($this->input->post('description'));
				$remarks =strtoupper($this->input->post('remarks'));
				$entryby =strtoupper($this->input->post('entryby'));
				$additional_data=array( 
				'name'=>strtoupper($this->input->post('name')),
				'description'=>strtoupper($this->input->post('description')),'remarks'=>strtoupper($this->input->post('remarks')),'entryby'=>strtoupper($this->input->post('entryby'))
				);
				$this->department_model->insert($name,$description,$remarks,$entryby,$additional_data);
				redirect("department/department/listing_department");
			
			}
			else{			
			//display the create user form
			//set the flash data error message if there is one
			$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

			$this->data['name'] = array(
				'name'  => 'name',
				'id'    => 'name',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('name'),
			);
			$this->data['description'] = array(
				'name'  => 'description',
				'id'    => 'description',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('description'),
			);
			$this->data['remarks'] = array(
				'name'  => 'remarks',
				'id'    => 'remarks',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('remarks'),
			);
			$this->data['entryby'] = array(
				'name'  => 'entryby',
				'id'    => 'entryby',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('entryby'),
			);
			//print_r($this->data);exit;

			$this->load->view('common/header',$meta);
			$this->load->view('department/create_department', $this->data);
			$this->load->view('common/footer');
			}			
		
		
		}
	function listing_department(){
			
			$meta['page_title']  = "Department List";
			$this->data['message'] = "department Added Successfully";
			$this->load->view('common/header',$meta);
			
			$this->load->view('department/view_department');
			
			$this->load->view('common/footer');		
		}
	
	function edit_department($id){
			$this->data['page_title'] = "Edit department";
			if (!$this->ion_auth->logged_in()){
			redirect('auth', 'refresh');
			}
			$user = $this->department_model->edit_department($id)->row();
			
			$this->form_validation->set_rules('name', 'department Name', 'required|xss_clean');
			$this->form_validation->set_rules('description', 'department Description', 'required|xss_clean');
			$this->form_validation->set_rules('remarks','department remarks','required|xss_clean');
			$this->form_validation->set_rules('entryby','department Entry By','required|xss_clean');
			if (isset($_POST) && !empty($_POST)){
				$data = array(
				'name' =>strtoupper( $this->input->post('name')),
				'description'  => strtoupper($this->input->post('description')),'remarks'=>strtoupper($this->input->post('remarks')),'entryby'=>strtoupper($this->input->post('entryby'))
				);
				if ($this->form_validation->run() === TRUE){
				$this->department_model->update($user->id, $data);
				redirect("department/department/listing_department");
				}
			}
			$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
			$this->data['user'] = $user;
			
			$this->data['name'] = array(
			'name'  => 'name',
			'id'    => 'name',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('name', $user->name),
			);
			$this->data['description'] = array(
			'name'  => 'description',
			'id'    => 'description',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('description', $user->description),
			);
			$this->data['remarks'] = array(
				'name'  => 'remarks',
				'id'    => 'remarks',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('remarks', $user->remarks),
			);
			$this->data['entryby'] = array(
				'name'  => 'entryby',
				'id'    => 'entryby',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('entryby', $user->entryby),
			);
			$this->data['submit'] = array(
			'name' => 'submit',
			'id'   => 'submit',
			'type' => 'submit',
			'value' =>'Save Edit',
			'class' =>'btn btn-success'
			);
			$meta['page_title'] = "Edit department";
			$this->load->view('common/header',$meta);
			$this->load->view('department/edit_department', $this->data, $id);
			$this->load->view('common/footer');
		}
		function delete_department($id){
		$this->department_model->delete($id);
		redirect("department/department/listing_department");		
		}
}
?>